﻿#include "openfile.h"
#include "ui_openfile.h"
#include "mainwindow.h"

QSize CustomTabStylenew::sizeFromContents(ContentsType type, const QStyleOption *option, const QSize &size, const QWidget *widget) const
{
    QSize s=QProxyStyle::sizeFromContents(type,option,size,widget);
    if(type==QStyle::CT_TabBarTab){
        s.transposed();
        s.rwidth()=168;
        s.rheight()=50;
    }
    return s;
}
void CustomTabStylenew::drawControl(ControlElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget) const
{
    if(element==CE_TabBarTabLabel){
        if(const QStyleOptionTab *tab=qstyleoption_cast<const QStyleOptionTab *>(option)){
            QRect allRect=tab->rect;
            if(tab->state&QStyle::State_Selected){
                painter->save();
                painter->setPen(0x89cfff);
                painter->setBrush(QBrush(0x89cfff));
                painter->drawRect(allRect.adjusted(6,6,-6,-6));
                painter->restore();
            }
            QTextOption option;
            option.setAlignment(Qt::AlignCenter);
            if(tab->state&QStyle::State_Selected){
                painter->setPen(0xf8fcff);
            }else{
                painter->setPen(0x000001);
            }
            painter->drawText(allRect,tab->text,option);
            return ;
        }
    }
    if(element==CE_TabBarTab){
        QProxyStyle::drawControl(element,option,painter,widget);
    }
}
openfile::openfile(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::openfile)
{
    ui->setupUi(this);
#ifdef Q_OS_ANDROID
    int fontId = QFontDatabase::addApplicationFont(":/FontStyle/AlibabaPuHuiTi-3-55-Regular.ttf");
    if (fontId != -1) {
        QStringList fontFamilies = QFontDatabase::applicationFontFamilies(fontId);
        if (!fontFamilies.isEmpty()) {
            QString familyName = fontFamilies.first();
            QFont font(familyName, 18); // 创建一个QFont对象，设置字体名称和默认大小

            // 使用setFont方法设置字体，传入QFont对象
            ui->tabWidget->setFont(font);
            ui->label_2->setFont(font);
            ui->label_3->setFont(font);
            ui->label_4->setFont(font);
            ui->label_5->setFont(font);
            ui->SaveBtn->setFont(font);


            // 如果需要为其他按钮设置不同的大小，可以创建新的QFont对象
            QFont smallerFont(familyName, 16);
            ui->returnBtn->setFont(smallerFont);
            ui->pushButton_9->setFont(smallerFont);

        }
    }
#endif
    ui->tabWidget->tabBar()->setStyle(new CustomTabStylenew);
}

openfile::~openfile()
{
    delete ui;
}

void openfile::on_SaveBtn_clicked()
{
    if(ui->tabWidget->currentIndex()==1){
        emit changeUploadWindow();
    }
    else if(ui->tabWidget->currentIndex()==0){
        QStringList sFilePath;


#ifdef Q_OS_ANDROID
        DialogFileManager dfm(0);
        dfm.exec();
        QString path = dfm.getResult();
        if(path != "0" && path != ""){
            if(path.contains("SMB://")){      // 网络路径文件缓存到本地
                SMBManager::I().downloadFile(path, "/storage/emulated/0/Download/" + path.split("/").back());
                sFilePath.append("/storage/emulated/0/Download/" + path.split("/").back());
            }else{
                sFilePath.append(path); // 安卓和本地文件直接输入路径
            }
        }

#else
        QFileDialog *fDialog=new QFileDialog(this,"file dlg","/home","All files(*.*)");
        if(fDialog->exec()==QFileDialog::Accepted)
        {
            sFilePath = fDialog->selectedFiles();
        }
#endif

        if(sFilePath.length()>0){

            QSqlDatabase db = dbHelper::openConnection();
            QSqlQuery query(db);
            MainWindow::experimentStatus.set(4);
            db.transaction();

            QString sql="Delete from SampleInfo";
            query.exec(sql);

            sql="Delete From AmplProgramTemp";
            query.exec(sql);

            sql="Delete From AmplStepTemp";
            query.exec(sql);

            sql="Delete From SampleAccessInfo";
            query.exec(sql);

            sql="Delete From ExperimentInfo";
            query.exec(sql);

            sql="Delete From FluorescenceData";
            query.exec(sql);

            QFile file(sFilePath[0]);
            QTextCodec* codec = QTextCodec::codecForName("GBK");
            file.open(QIODevice::ReadOnly | QIODevice::Text);

            QTextStream txtInput(&file);
            txtInput.setCodec(codec);
            while (!txtInput.atEnd())
            {
                QString lineStr = txtInput.readLine();
                query.exec(lineStr);
            }

            db.commit();

            sql="Select count(*) Sum from FluorescenceData;";

            query.exec(sql);
            bool dataStatus=false;
            while (query.next()) {
                dataStatus=query.value("Sum").toInt()==0;
            }

            if(dataStatus){
//            if(false){
                MainWindow::experimentStatus.set(5);
                sql="Update ExperimentInfo Set AmplGuid=''";
                query.exec(sql);
                this->hide();
                emit changeSampleWindow();
            }else {
                MainWindow::experimentStatus.set(4);
                this->hide();
                emit changeAnalyWindow(sFilePath[0].split('/').back());
            }
        }
    }
}

void openfile::on_returnBtn_clicked()
{
    this->hide();
    emit changeMainWindow();
}
